--- /dev/null
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GDK_MEMORY_TEXTURE__H__
+#define __GDK_MEMORY_TEXTURE__H__
+
+#if !defined (__GDK_H_INSIDE__) && !defined (GDK_COMPILATION)
+#error "Only <gdk/gdk.h> can be included directly."
+#endif
+
+#include <gdk/gdktexture.h>
+
+G_BEGIN_DECLS
+
+/*
+ * GdkMemoryFormat:
+ * @GDK_MEMORY_B8G8R8A8_PREMULTIPLIED: 4 bytes; for blue, green, red, alpha.
+ * The color values are premultiplied with the alpha value.
+ * @GDK_MEMORY_A8R8G8B8_PREMULTIPLIED: 4 bytes; for alpha, red, green, blue.
+ * The color values are premultiplied with the alpha value.
+ * @GDK_MEMORY_B8G8R8A8: 4 bytes; for blue, green, red, alpha.
+ * @GDK_MEMORY_A8R8G8B8: 4 bytes; for alpha, red, green, blue.
+ * @GDK_MEMORY_R8G8B8A8: 4 bytes; for red, green, blue, alpha.
+ * @GDK_MEMORY_A8B8G8R8: 4 bytes; for alpha, blue, green, red.
+ * @GDK_MEMORY_R8G8B8: 3 bytes; for red, green, blue. The data is opaque.
+ * @GDK_MEMORY_B8G8R8: 3 bytes; for blue, green, red. The data is opaque.
+ * @GDK_MEMORY_N_FORMATS: The number of formats. This value will change as
+ * more formats get added, so do not rely on its concrete integer.
+ *
+ * #GdkMemoryFormat describes a format that bytes can have in memory.
+ *
+ * It describes formats by listing the contents of the memory passed to it.
+ * So GDK_MEMORY_A8R8G8B8 will be 1 byte (8 bits) of alpha, followed by a
+ * byte each of red, green and blue. It is not endian-dependant, so
+ * CAIRO_FORMAT_ARGB32 is represented by different #GdkMemoryFormats on
+ * architectures with different endiannesses.
+ *
+ * Its naming is modelled after VkFormat (see
+ * https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#VkFormat
+ * for details).
+ */
+typedef enum {
+ GDK_MEMORY_B8G8R8A8_PREMULTIPLIED,
+ GDK_MEMORY_A8R8G8B8_PREMULTIPLIED,
+ GDK_MEMORY_B8G8R8A8,
+ GDK_MEMORY_A8R8G8B8,
+ GDK_MEMORY_R8G8B8A8,
+ GDK_MEMORY_A8B8G8R8,
+ GDK_MEMORY_R8G8B8,
+ GDK_MEMORY_B8G8R8,
+
+ GDK_MEMORY_N_FORMATS
+} GdkMemoryFormat;
+
+GDK_AVAILABLE_IN_ALL
+GdkTexture * gdk_memory_texture_new (int width,
+ int height,
+ GdkMemoryFormat format,
+ GBytes *bytes,
+ gsize stride);
+
+
+G_END_DECLS
+
+#endif /* __GDK_MEMORY_TEXTURE_H__ */
--- /dev/null
+#include <locale.h>
+#include <gdk/gdk.h>
+
+/* maximum bytes per pixel */
+#define MAX_BPP 4
+
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+#define GDK_MEMORY_CAIRO_FORMAT_ARGB32 GDK_MEMORY_B8G8R8A8_PREMULTIPLIED
+#elif G_BYTE_ORDER == G_BIG_ENDIAN
+#define GDK_MEMORY_CAIRO_FORMAT_ARGB32 GDK_MEMORY_A8R8G8B8_PREMULTIPLIED
+#endif
+
+typedef enum {
+ BLUE,
+ GREEN,
+ RED,
+ TRANSPARENT,
+ ALMOST_OPAQUE_REBECCAPURPLE,
+ N_COLORS
+} Color;
+
+const char * color_names[N_COLORS] = {
+ "blue",
+ "green",
+ "red",
+ "transparent",
+ "almost_opaque_rebeccapurple"
+};
+
+typedef struct _MemoryData {
+ gsize bytes_per_pixel;
+ guint opaque : 1;
+ guchar data[N_COLORS][MAX_BPP];
+} MemoryData;
+
+typedef struct _TestData {
+ GdkMemoryFormat format;
+ Color color;
+} TestData;
+
+#define RGBA(a, b, c, d) { 0x ## a, 0x ## b, 0x ## c, 0x ## d }
+
+static MemoryData tests[GDK_MEMORY_N_FORMATS] = {
+ { 4, FALSE, { RGBA(FF,00,00,FF), RGBA(00,FF,00,FF), RGBA(00,00,FF,FF), RGBA(00,00,00,00), RGBA(66,22,44,AA) } },
+ { 4, FALSE, { RGBA(FF,00,00,FF), RGBA(FF,00,FF,00), RGBA(FF,FF,00,00), RGBA(00,00,00,00), RGBA(AA,44,22,66) } },
+ { 4, FALSE, { RGBA(FF,00,00,FF), RGBA(00,FF,00,FF), RGBA(00,00,FF,FF), RGBA(00,00,00,00), RGBA(99,33,66,AA) } },
+ { 4, FALSE, { RGBA(FF,00,00,FF), RGBA(FF,00,FF,00), RGBA(FF,FF,00,00), RGBA(00,00,00,00), RGBA(AA,66,33,99) } },
+ { 4, FALSE, { RGBA(00,00,FF,FF), RGBA(00,FF,00,FF), RGBA(FF,00,00,FF), RGBA(00,00,00,00), RGBA(66,33,99,AA) } },
+ { 4, FALSE, { RGBA(FF,FF,00,00), RGBA(FF,00,FF,00), RGBA(FF,00,00,FF), RGBA(00,00,00,00), RGBA(AA,99,33,66) } },
+ { 3, TRUE, { RGBA(00,00,FF,00), RGBA(00,FF,00,00), RGBA(FF,00,00,00), RGBA(00,00,00,00), RGBA(44,22,66,00) } },
+ { 3, TRUE, { RGBA(FF,00,00,00), RGBA(00,FF,00,00), RGBA(00,00,FF,00), RGBA(00,00,00,00), RGBA(66,22,44,00) } },
+};
+
+static void
+compare_textures (GdkTexture *expected,
+ GdkTexture *test,
+ gboolean ignore_alpha)
+{
+ guchar *expected_data, *test_data;
+ gint width, height;
+ gint x, y;
+
+ g_assert_cmpint (gdk_texture_get_width (expected), ==, gdk_texture_get_width (test));
+ g_assert_cmpint (gdk_texture_get_height (expected), ==, gdk_texture_get_height (test));
+
+ width = gdk_texture_get_width (expected);
+ height = gdk_texture_get_height (expected);
+
+ expected_data = g_malloc (width * height * 4);
+ gdk_texture_download (expected, expected_data, width * 4);
+
+ test_data = g_malloc (width * height * 4);
+ gdk_texture_download (test, test_data, width * 4);
+
+ for (y = 0; y < height; y++)
+ {
+ for (x = 0; x < width; x++)
+ {
+ if (ignore_alpha)
+ g_assert_cmphex (*(guint32 *) &expected_data[y * width + x * 4] & 0xFFFFFF, ==, *(guint32 *) &test_data[y * width + x * 4] & 0xFFFFFF);
+ else
+ g_assert_cmphex (*(guint32 *) &expected_data[y * width + x * 4], ==, *(guint32 *) &test_data[y * width + x * 4]);
+ }
+ }
+
+ g_free (expected_data);
+ g_free (test_data);
+}
+
+static GdkTexture *
+create_texture (GdkMemoryFormat format,
+ Color color,
+ int width,
+ int height,
+ gsize stride)
+{
+ GdkTexture *texture;
+ GBytes *bytes;
+ guchar *data;
+ int x, y;
+
+ data = g_malloc (height * stride);
+ for (y = 0; y < height; y++)
+ for (x = 0; x < width; x++)
+ {
+ memcpy (&data[y * stride + x * tests[format].bytes_per_pixel],
+ &tests[format].data[color],
+ tests[format].bytes_per_pixel);
+ }
+
+ bytes = g_bytes_new_static (data, height * stride);
+ texture = gdk_memory_texture_new (width, height,
+ format,
+ bytes,
+ stride);
+ g_bytes_unref (bytes);
+
+ return texture;
+}
+
+static void
+test_download_1x1 (gconstpointer data)
+{
+ const TestData *test_data = data;
+ GdkTexture *expected, *test;
+
+ expected = create_texture (GDK_MEMORY_CAIRO_FORMAT_ARGB32, test_data->color, 1, 1, tests[test_data->format].bytes_per_pixel);
+ test = create_texture (test_data->format, test_data->color, 1, 1, tests[test_data->format].bytes_per_pixel);
+
+ compare_textures (expected, test, tests[test_data->format].opaque);
+
+ g_object_unref (expected);
+ g_object_unref (test);
+}
+
+static void
+test_download_1x1_with_stride (gconstpointer data)
+{
+ const TestData *test_data = data;
+ GdkTexture *expected, *test;
+
+ expected = create_texture (GDK_MEMORY_CAIRO_FORMAT_ARGB32, test_data->color, 1, 1, 4);
+ test = create_texture (test_data->format, test_data->color, 1, 1, 2 * MAX_BPP);
+
+ compare_textures (expected, test, tests[test_data->format].opaque);
+
+ g_object_unref (expected);
+ g_object_unref (test);
+}
+
+static void
+test_download_4x4 (gconstpointer data)
+{
+ const TestData *test_data = data;
+ GdkTexture *expected, *test;
+
+ expected = create_texture (GDK_MEMORY_CAIRO_FORMAT_ARGB32, test_data->color, 4, 4, 16);
+ test = create_texture (test_data->format, test_data->color, 4, 4, 4 * tests[test_data->format].bytes_per_pixel);
+
+ compare_textures (expected, test, tests[test_data->format].opaque);
+
+ g_object_unref (expected);
+ g_object_unref (test);
+}
+
+static void
+test_download_4x4_with_stride (gconstpointer data)
+{
+ const TestData *test_data = data;
+ GdkTexture *expected, *test;
+
+ expected = create_texture (GDK_MEMORY_CAIRO_FORMAT_ARGB32, test_data->color, 4, 4, 16);
+ test = create_texture (test_data->format, test_data->color, 4, 4, 4 * MAX_BPP);
+
+ compare_textures (expected, test, tests[test_data->format].opaque);
+
+ g_object_unref (expected);
+ g_object_unref (test);
+}
+
+int
+main (int argc, char *argv[])
+{
+ GdkMemoryFormat format;
+ Color color;
+ GEnumClass *enum_class;
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_bug_base ("http://bugzilla.gnome.org");
+
+ enum_class = g_type_class_ref (GDK_TYPE_MEMORY_FORMAT);
+
+ for (format = 0; format < GDK_MEMORY_N_FORMATS; format++)
+ {
+ for (color = 0; color < N_COLORS; color++)
+ {
+ TestData *test_data = g_new (TestData, 1);
+ char *test_name = g_strdup_printf ("/memorytexture/download_1x1/%s/%s",
+ g_enum_get_value (enum_class, format)->value_nick,
+ color_names[color]);
+ test_data->format = format;
+ test_data->color = color;
+ g_test_add_data_func_full (test_name, test_data, test_download_1x1, g_free);
+ g_free (test_name);
+
+ test_data = g_new (TestData, 1);
+ test_name = g_strdup_printf ("/memorytexture/download_1x1_with_stride/%s/%s",
+ g_enum_get_value (enum_class, format)->value_nick,
+ color_names[color]);
+ test_data->format = format;
+ test_data->color = color;
+ g_test_add_data_func_full (test_name, test_data, test_download_1x1_with_stride, g_free);
+ g_free (test_name);
+
+ test_data = g_new (TestData, 1);
+ test_name = g_strdup_printf ("/memorytexture/download_4x4/%s/%s",
+ g_enum_get_value (enum_class, format)->value_nick,
+ color_names[color]);
+ test_data->format = format;
+ test_data->color = color;
+ g_test_add_data_func_full (test_name, test_data, test_download_4x4, g_free);
+ g_free (test_name);
+
+ test_data = g_new (TestData, 1);
+ test_name = g_strdup_printf ("/memorytexture/download_4x4_with_stride/%s/%s",
+ g_enum_get_value (enum_class, format)->value_nick,
+ color_names[color]);
+ test_data->format = format;
+ test_data->color = color;
+ g_test_add_data_func_full (test_name, test_data, test_download_4x4_with_stride, g_free);
+ g_free (test_name);
+ }
+ }
+
+ return g_test_run ();
+}